{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 判断n是否是质数\n",
    "质数：只有1和它本身两个约数的数"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "步骤：\n",
    "1、判断n是否等于2：如果n=2,n是质数；\n",
    "2、如果n不等于2，做进一步处理；用n除以2,3,...，n-1。只要其中有一数能整除n，则n不是质数；\n",
    "3、否则，n是质数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入一个整数:7\n",
      "7 是质数\n"
     ]
    }
   ],
   "source": [
    "n = int(input(\"请输入一个整数:\"))\n",
    "if(n != 2):\n",
    "    for i in (2, n):\n",
    "        if (n%i)==0:\n",
    "            print(n,'不是质数')\n",
    "            break\n",
    "        else:\n",
    "            print(n, '是质数')\n",
    "            break\n",
    "else:\n",
    "    print(n, '不是质数')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 用二分法求解方程的近似根\n",
    "方程：$x²-2=0$  \n",
    "所求近似根与精确解的差不超过0.005"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "x1、x2之间的任意值均为满足条件的近似值\n"
     ]
    }
   ],
   "source": [
    "x1, x2 = 1, 2\n",
    "\n",
    "def f(x):\n",
    "    return pow(x, 2) - 2\n",
    "\n",
    "while(abs(x1-x2) > 0.005):\n",
    "    m = (x1+x2)/2\n",
    "    if f(m)== 0:\n",
    "        print(m, '为所求')\n",
    "    else:\n",
    "        xm = f(x1) * f(m)\n",
    "        if xm > 0:\n",
    "            x1 = m\n",
    "        else:\n",
    "            x2 = m\n",
    "\n",
    "print('x1、x2之间的任意值均为满足条件的近似值')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [],
   "source": [
    "x1, x2 = 1, 2\n",
    "\n",
    "def f(x):\n",
    "    return pow(x, 2) - 2\n",
    "\n",
    "\n",
    "if abs(x1-x2)<0.005:\n",
    "    print('x1、x2之间的任意值均为满足条件的近似值')\n",
    "else:\n",
    "    m = (x1+x2)/2\n",
    "    if f(m)== 0:\n",
    "        print(m, '为所求')\n",
    "    else:\n",
    "        xm = f(x1) * f(m)\n",
    "        if xm > 0:\n",
    "            x1 = m\n",
    "        else:\n",
    "            x2 = m"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "unexpected EOF while parsing (<ipython-input-38-9012c83a42ff>, line 3)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  File \u001b[1;32m\"<ipython-input-38-9012c83a42ff>\"\u001b[1;36m, line \u001b[1;32m3\u001b[0m\n\u001b[1;33m    def fichotomy(a, b, f, r):\u001b[0m\n\u001b[1;37m                              ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m unexpected EOF while parsing\n"
     ]
    }
   ],
   "source": [
    "def f(x):\n",
    "    return pow(x, 2) -2\n",
    "def fichotomy(a, b, f, r):\n",
    "    num1 = f(a)\n",
    "    num2 = f(b)\n",
    "    if num1>num2:\n",
    "        print('该区间不存在值')\n",
    "        return None\n",
    "    n_num, p_num = (a,b) if num < num2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
